#ifndef SOCKET_H_
#define SOCKET_H_

/***
    Amrita ITEWS - Copyright (C) 2009 Amrita Vishwa Vidyapeetham, Amritapuri.
                                       (http://www.amrita.edu)
    ***************************************************************************
    This file is part of the Amrita ITEWS distribution.
    Amrita ITEWS is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License (version 2) as published
    by the Free Software Foundation AND MODIFIED BY the Amrita ITEWS exception.
    ***NOTE*** The exception to the GPL is included to allow you to distribute
    a combined work that includes Amrita ITEWS without being obliged to provide
    the source code for proprietary components outside of the Amrita ITEWS
    software. Amrita ITEWS is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    Public License for more details. You should have received a copy of the GNU
    General Public License and the Amrita ITEWS license exception along with
    Amrita ITEWS if not then it can be viewed here:
    http://itews.amrita.ac.in/license.html.

    Note: This software is closely coupled to FreeRTOS and UIP and hence the
    licensing terms of FreeRTOS and UIP would apply.


    Documentation, latest information, license and contact details are at:
    http://itews.amrita.ac.in/


    Amrita ITEWS source code can be found at:
    http://code.google.com/p/itews


    The initial code-base of Amrita ITEWS was developed by Amrita Vishwa
    Vidyapeetham as part of the project titled,"Intelligent & Interactive
    Telematics using Emerging Wireless Technologies for Transport Systems
    (ITEWS)" sponsored by Technology Information Forecasting and Assessment
    Council (TIFAC), India.
***/

/*
A socket interface to UIP running as a TCP/IP server task under FreeRTOS.  Developed
by Amrita Research Labs as part of ITEWS project.
*/

#include <stream_eventq.h>
#include <dgram_eventq.h>
#include <uip-conf.h>

#define PROTOCOL_HDR_SIZE (14+32)

typedef struct tag_socket_buf
{
    uint8                   buf[UIP_CONF_BUFFER_SIZE-PROTOCOL_HDR_SIZE];
    uint32                  pos;
    uint32                  count;
}socket_buf_t;

typedef struct tag_socket_udp_msg
{
    uint8   *buf;
    int32    count;
    uint8    ipbuf[4];
    uint16   port;
}socket_udp_msg_t;

struct uip_conn;

typedef struct tag_socket
{
    uint16                  lport;
    socket_buf_t            ibuf;/*input buffer*/
    uint32                  state;
    int32                   close_status;
    int32                   write_count;
    struct timer            rw_timer;
    void                    *req_buf;
    int32                   req_count;
    wait_struct_t           *req_ws;//wait struct for tasks to block on the socket
    struct uip_conn         *conn;//back-ref to associate uip_conn for active sockets
    struct uip_udp_conn     *udp_conn;
    struct  tag_socket      *next;//for lists
    HMUTEX                  mutex;
}socket_t;

#define SOCKET_MAX              2

int32 socket_init(const char *ip, const char *netmask, const uint8 *mac, const char *dr);

int32 _socket_open();

int32 _socket_bind(int fd, uint16 port);

int32 _socket_listen(int fd);

int32 _socket_accept(int fd, stream_param_t *buf);

int32 _socket_connect(int fd, stream_param_t *buf);

int32 _socket_read(int fd, uint8 *buf, int32 len);

int32 _socket_write(int fd, uint8 *buf, int32 len);

int32 _socket_setopt(int fd, int32 opt, void *val);

int32 _socket_close(int fd);

int32 _socket_udp_connect(int fd, dgram_param_t *param);

int32 _socket_udp_send_to(int fd, uint8 *buf, int32 len, dgram_param_t *param);

void socket_process_pending_lists_();

void socket_process_new_connect_();

void socket_process_close_();

void socket_process_timeout_();

void socket_process_try_restart_();

void socket_process_write_();

void socket_process_new_data_();


#endif /*SOCKET_H_*/
